﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.DetailsManager
{
    public partial class GrandPermissionGroupUC : DetailUC
    {
        public GrandPermissionGroupUC()
        {
            InitializeComponent();
            this.Code = "GrandPermission";
            this.ObjectName = "phần quyền";
            this.Type = new Group_Module_Role();
            comboboxUC_module.Model = RegisterService.RegisterServices.ModuleService;
            comboboxUC_module.Uc = new ModuleUC();
            comboboxUC_module.ColumnMain = "Name";
            comboboxUC_module.ColumnDisplay = "Code,Name,Description";
        }
    
        protected override long DetailDisplay()
        {
            Group_Module_Role role = new Group_Module_Role();
            if (ID > 0)
            {
                role = (Group_Module_Role)RegisterService.RegisterServices.Group_Module_RoleService.GetByID(ID);
            }
           // Module module = RegisterService.RegisterServices.ModuleService.GetByGuidID(role.ModuleID);
           // if (module != null)
           // {
            comboboxUC_module.ID = role.ModuleID;
           // }
           // else
           // {
               // comboboxUC_module.ID = -1;
           // }
            if (role.Role != null)
            {
                ckviewdata.Checked = role.Role.Contains("R");
                ckupdatedata.Checked = role.Role.Contains("C") || role.Role.Contains("U");
                ckdeletedata.Checked = role.Role.Contains("D");
                ckexportdata.Checked = role.Role.Contains("P") || role.Role.Contains("E");
            }else
            {
                ckviewdata.Checked = false;
                ckupdatedata.Checked = false;
                ckdeletedata.Checked = false;
                ckexportdata.Checked = false;
            }
            return ID;
        }

        protected override long Save()
        {
            Group_Module_Role role = new Group_Module_Role();
            if (ID > 0)
            {
                role = (Group_Module_Role)RegisterService.RegisterServices.Group_Module_RoleService.GetByID(ID);
            }
            role.Role = "";
            role.GroupID = long.Parse(SearchString.Replace("'", "").Replace("GroupID=", "").Trim());
            role.ModuleID = comboboxUC_module.ID;
            if (ckupdatedata.Checked)
            {
                role.Role += "C";
                if (ckviewdata.Checked)
                    role.Role += "R";
                role.Role += "U";
            }
            if (ckdeletedata.Checked)
            {
                role.Role += "D";
            }
            if (ckexportdata.Checked)
            {
                role.Role += "PE";
            }
            ID = RegisterService.RegisterServices.Group_Module_RoleService.Save(role);

            return ID;
        }

        public override bool Delete(long SelectedID)
        {
            return RegisterService.RegisterServices.Group_Module_RoleService.Delete((Group_Module_Role)RegisterService.RegisterServices.Group_Module_RoleService.GetByID(SelectedID));

        }
        private void GrandPermissionGroupUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(groupControl1.Controls);
            AddTextChanged(groupControl2.Controls);
        }

    }
}
